package edu.sctu;


import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;

public class timeFilter implements Filter {
    private FilterConfig filterConfig=null;
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        this.filterConfig=filterConfig;

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse
            servletResponse, FilterChain filterChain) throws IOException, ServletException {
//        servletRequest.setCharacterEncoding("UTF-8");
//        servletRequest.setCharacterEncoding("UTF-8");
//        servletResponse.setContentType("text/html;charset=UTF-8");
//        PrintWriter out = servletResponse.getWriter();
        long starTime = System.currentTimeMillis();
//        out.println("<h1>"+starTime+"</h1>");
        HttpServletRequest request= (HttpServletRequest)servletRequest;
        HttpServletResponse response=(HttpServletResponse)servletResponse;
        HttpSession session=request.getSession();
        session.setAttribute("starTime",starTime);
        request.getRequestDispatcher("/time.jsp").forward(request,response);
        filterChain.doFilter(request,response);

    }

    @Override
    public void destroy() {
        this.filterConfig=null;

    }
}
